//
//  ShougongViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/27.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ShougongViewController.h"
#import "CollectionHeaderCollectionViewCell.h"
#import "CollectionheaderModel.h"
#import "WebViewViewController.h"
@interface ShougongViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain) UICollectionView *contentView;//collectionView
@property (nonatomic, retain) NSMutableArray *dataArr;//collection的数据
@end

@implementation ShougongViewController

-(void)dealloc
{
    [_contentView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor purpleColor];
    [self data];
#pragma mark - 创建Layout
    UICollectionViewFlowLayout *contentLayout = [[UICollectionViewFlowLayout alloc]init];
    //行列间距
    contentLayout.minimumInteritemSpacing = 10;
    contentLayout.minimumLineSpacing = 10;
    //item大小
    contentLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 20, 200);
    //边界
    contentLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
#pragma mark - 创建collectionView
    self.contentView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:contentLayout];
    //代理
    self.contentView.delegate = self;
    self.contentView.dataSource = self;
    //背景颜色
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.contentView];
    [_contentView release];
    //重用池
    [_contentView registerClass:[CollectionHeaderCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [contentLayout release];
    //刷新加载
    [self creatUI];
}
#pragma mark - collectionView必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    CollectionheaderModel *collHeader = _dataArr[indexPath.item];
    [cell.bgimg sd_setImageWithURL:[NSURL URLWithString:collHeader.picurl]];//图片
    cell.title.text = collHeader.title;//标题
    cell.price.text =[@"￥" stringByAppendingString:collHeader.yh_price];//价格
    cell.oldprice.text = [@"￥" stringByAppendingString:collHeader.price];//原价
    cell.num.text = [collHeader.shop_type stringByAppendingString:@"人已买"];//购买数量
    
    return cell;
}
#pragma mark - 数据解析
- (void)data
{

    
    NSString *str = @"http://course.wx.jaeapp.com/index.php?c=market&a=products&cate=0&page=1&pagesize=20&sort=0&subcate=0&vid=15";
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"];
        self.dataArr = [CollectionheaderModel modelHandlerWithArray:arr];
        [_contentView reloadData];
    }];
    
   
}

#pragma mark - collectionView 跳转页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewViewController *web = [[WebViewViewController alloc]init];
    CollectionheaderModel *c = _dataArr[indexPath.item];
    web.strid = c.num_iid;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark - 刷新加载
-(void)creatUI
{
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self data];
    }];
    [header setTitle:@"我刷我刷我刷刷刷" forState:MJRefreshStatePulling];
    [header beginRefreshing];//开始刷新
    self.contentView.mj_header = header;
    //加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _page ++;
        NSString * str = [[@"http://course.wx.jaeapp.com/index.php?c=market&a=products&cate=0&" stringByAppendingString:[NSString stringWithFormat:@"page=%ld",_page]]stringByAppendingString:@"&pagesize=20&sort=0&subcate=0&vid=15"];
        [AFNnetWork GETwithURL:str completion:^(id result) {
            NSArray *arr = result[@"data"];
            NSArray *atemp = [CollectionheaderModel modelHandlerWithArray:arr];
            for (CollectionheaderModel *f in atemp) {
                [self.dataArr addObject:f];//添加到可变数组里
            }
            [self.contentView reloadData];
        }];
        [self.contentView.mj_footer endRefreshing];
        
    }];
    self.contentView.mj_footer = footer;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
